package cn.lxh.mq.consumer;

import cn.lxh.constants.Constants;
import cn.lxh.pojo.app.bo.PayResultBO;
import cn.lxh.service.IOrderService;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;


@RocketMQMessageListener(
        consumerGroup= Constants.MQ.CONSUMER_PAY_RESULT,
        topic = Constants.MQ.TOPIC_PAY_RESULT,
        selectorExpression = Constants.MQ.TAGS_PAY_RESULT,
        //并行消费  ORDERLY ：顺序消费  ； CONCURRENTLY：并发消费
        consumeMode= ConsumeMode.CONCURRENTLY,
        //消息的模式：CLUSTERING 集群 ，  BROADCASTING：广播
        messageModel= MessageModel.CLUSTERING
)
@Component
public class PayResultConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    private IOrderService orderService ;

    @Override
    public void onMessage(MessageExt message) {
        //拿到支付结果
        byte[] body = message.getBody();
        String jsonStr = new String(body, StandardCharsets.UTF_8);
        PayResultBO payResultBO = JSON.parseObject(jsonStr, PayResultBO.class);
        //处理支付结果
        orderService.handlerPayResult(payResultBO);
    }
}
